package cn.edu.lingnan.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

//    处理浏览器的请求
//    以前的写法
//    @RequestMapping(value = "/user/login", method = RequestMethod.POST)

    @PostMapping(value = "/user/login")
    public String login(@RequestParam(value = "username",required = false) String username,
                        @RequestParam(value = "password",required = false) String password,
                        Map<String,Object> map,
                        HttpSession session){

        if (!StringUtils.isEmpty(username) && password.equals("123456")){

//          登陆成功，为了防止表单重复提交，可以重定向主页
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }else{
            map.put("msg","登陆失败！用户名或密码错误！");
            return "redirect:/login.html";
        }
    }
}
